package com.changgou.goods.service.impl;

import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 13545
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/10/31 14:38
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 根据分类ID查询品牌集合
     * @param categoryid
     * @return
     */
    @Override
    public List<Brand> findByCategory(Integer categoryid) {
        List<Brand> brandList = brandMapper.findByCategory(categoryid);
        return brandList;
    }

    /**
     * 分页+条件搜索
     * @param page 当前页
     * @param size 每页显示的条数
     * @param brand 查询条件
     * @return
     */
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size, Brand brand) {
        Example example = createExample(brand);
        PageHelper.startPage(page,size);
        List<Brand> brands = brandMapper.selectByExample(example);
        return new PageInfo<>(brands);
    }

    /**
     * 条件搜索
     * @param page 当前页
     * @param size 每页显示的条数
     * @return
     */
    @Override
    public PageInfo<Brand> findPage(Integer page, Integer size) {
        //分页实现
        PageHelper.startPage(page,size);
        //查询集合
        List<Brand> brands = brandMapper.selectAll();
        //封装pageinfo
        return new PageInfo<Brand>(brands);
    }

    /**
     * 多条件搜索
     * @param brand
     * @return
     */
    @Override
    public List<Brand> findList(Brand brand) {
        Example example = createExample(brand);
        return brandMapper.selectByExample(example);
    }

    private Example createExample(Brand brand){
        //自定义条件搜索对象 Example
        Example example = new Example(Brand.class);
        //条件构造器
        Example.Criteria criteria = example.createCriteria();
        if(brand != null){
            //brand.name != null 根据名字模糊搜索 where name like '%华%'
            if(StringUtils.isNotEmpty(brand.getName())){
                /**
                 * 1.Brand的属性名
                 * 2.占位符参数，搜索的条件
                 */
                criteria.andLike("name","%"+brand.getName()+"%");
            }
            //brand.letter != null 根据首字母搜索
            if(!StringUtils.isNotEmpty(brand.getLetter())){
                criteria.andEqualTo("letter",brand.getLetter());
            }
        }
        return example;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id) {
        return  brandMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    /**
     * 修改
     * @param brand
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }
}
